// pages/home-page/customer-detail/customerDetail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CDNPATH: app.globalData.CDNPATH,
    show: 'tag',
    age: 0,
    sex: '',
    id: '',
    tagList: [],
    hobbyList: [],
    comment: '',
    personalInfo: {},
    members: [],
    boxShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCustomerTag()
    this.getPersonalInfo()
    this.getFamilyInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getCustomerTag: function (params) {
    app.api.post({
      url: 'customer_tag_list',
      data: {
        customerId: this.data.id
      }
    }).then(res => {
      let arr1,arr2
      arr1 = res.tagList.filter(item => item.type!=='hobby')
      arr2 = res.tagList.filter(item => item.type==='hobby')
      if (arr2.length>0) {
        arr1.push(arr2[0])
      }
      this.setData({
        hobbyList: arr2,
        tagList: arr1,
        age: res.age,
        sex: res.sex
      })
    })
    app.api.post({
      url: 'customer_tag_customerComment',
      data: {
        id: this.data.id
      }
    }).then(res => {
      this.setData({
        comment: res.remark
      })
    })
  },
  getPersonalInfo: function (params) {
    app.api.post({
      url: 'customer_manage_detail',
      data: {
        id: this.data.id
      }
    }).then(res => {
      this.setData({
        personalInfo: res
      })
    })
  },
  getFamilyInfo: function (params) {
    app.api.post({
      url: 'customer_contact_list',
      data: {
        customerId: this.data.id
      }
    }).then(res => {
      this.setData({
        members: res || []
      })
    })
  },
  switchTab: function (e) {
    const { key } = e.currentTarget.dataset
    this.setData({
      show: key
    })
  },
  editTags: function (params) {
    wx.navigateTo({
      url: `/pages/home-page/customer-tag/customerTag?customerId=${this.data.id}`
    })
  },
  editPersonal: function (params) {
    wx.navigateTo({
      url: `/pages/home-page/edit-customer/editCustomer?info=${JSON.stringify(this.data.personalInfo)}`
    })
  },
  editFamily: function (params) {
    wx.navigateTo({
      url: `/pages/home-page/edit-family/editFamily?customerId=${this.data.id}&members=${JSON.stringify(this.data.members)}`
    })
  },
  openHobbyBox: function(e) {
    const { type } = e.currentTarget.dataset
    console.log(type)
    if (type==='hobby'&&this.data.hobbyList.length>0) {
      this.setData({
        boxShow: true
      })
    }
  },
  closeBoxShow: function(e) {
    this.setData({
      boxShow: false
    })
  }
})